﻿using System;
using System.Collections.Generic;
using System.Text;

//CID 0797 //2011.02.28 //Begin
namespace InfoJetSoft.Service.XPath.Exprs
{
    class NodeSetFunctions
    {
        public static double last(ExprContext context, object args)
        {
            //是否就是在父节点中的位置?
            return double.NaN;
        }

        public static double position(ExprContext context, object args)
        {
            //是否就是在父节点中的位置?
            return double.NaN;
        }

        public static double count(ExprContext context, object arg)
        {
            if (arg is INodeList)
            {
                INodeList nodeList = (INodeList)arg;
                return nodeList.Count;
            }
            else
            {
                throw new ExprException("Wrong parameters in count().");
            }
        }

        public static string id(ExprContext context, object arg)
        {
            return "None";
        }

        public static string localName(ExprContext context, object arg)
        {
            if (arg is INodeList)
            {
                INodeList nodeList = (INodeList)arg;
                if (nodeList.Count <= 0)
                {
                    return "";
                }
                else
                {
                    object node = nodeList.Get(0);
                    return context.Calculator.GetNodeLocalName(node);
                }
            }
            else
            {
                throw new ExprException("Wrong parameters in count().");
            }
        }

        public static string namespaceUri(ExprContext context, object arg)
        {
            if (arg is INodeList)
            {
                INodeList nodeList = (INodeList)arg;
                if (nodeList.Count <= 0)
                {
                    return "";
                }
                else
                {
                    object node = nodeList.Get(0);
                    string uri = context.Calculator.GetNodeNamespaceUri(node);
                    if (uri == null)
                    {
                        uri = "";
                    }
                    return uri;
                }
            }
            else
            {
                throw new ExprException("Wrong parameters in namespace-uri().");
            }
        }

        public static string name(ExprContext context, object arg)
        {
            if (arg is INodeList)
            {
                INodeList nodeList = (INodeList)arg;
                if (nodeList.Count <= 0)
                {
                    return "";
                }
                else
                {
                    object node = nodeList.Get(0);
                    return context.Calculator.GetNodeName(node);
                }
            }
            else
            {
                throw new ExprException("Wrong parameters in name().");
            }
        }

        public static object CallFunction(ExprContext context, string funcName, List<object> args)
        {
            if (funcName.Equals("last"))
            {
                if (args.Count == 0)
                {
                    object contextNodes = context.Calculator.GetContextNodes(false);
                    return NodeSetFunctions.last(context, contextNodes);
                }
                else
                {
                    throw new ExprException("Wrong parameters in last().");
                }
            }
            else if (funcName.Equals("position"))
            {
                if (args.Count == 0)
                {
                    object contextNodes = context.Calculator.GetContextNodes(false);
                    return NodeSetFunctions.position(context, contextNodes);
                }
                else
                {
                    throw new ExprException("Wrong parameters in position().");
                }
            }
            else if (funcName.Equals("count"))
            {
                if (args.Count == 1)
                {
                    return NodeSetFunctions.count(context, args[0]);
                }
                else
                {
                    throw new ExprException("Wrong parameters in count().");
                }
            }
            else if (funcName.Equals("id"))
            {
                if (args.Count == 1)
                {
                    return NodeSetFunctions.id(context, args[0]);
                }
                else
                {
                    throw new ExprException("Wrong parameters in id().");
                }
            }
            else if (funcName.Equals("local-name"))
            {
                if (args.Count == 0)
                {
                    object contextNodes = context.Calculator.GetContextNodes(false);
                    return NodeSetFunctions.localName(context, contextNodes);
                }
                else if (args.Count == 1)
                {
                    return NodeSetFunctions.localName(context, args[0]);
                }
                else
                {
                    throw new ExprException("Wrong parameters in local-name().");
                }
            }
            else if (funcName.Equals("namespace-uri"))
            {
                if (args.Count == 0)
                {
                    object contextNodes = context.Calculator.GetContextNodes(false);
                    return NodeSetFunctions.namespaceUri(context, contextNodes);
                }
                else if (args.Count == 1)
                {
                    return NodeSetFunctions.namespaceUri(context, args[0]);
                }
                else
                {
                    throw new ExprException("Wrong parameters in namespace-uri().");
                }
            }
            else if (funcName.Equals("name"))
            {
                if (args.Count == 0)
                {
                    object contextNodes = context.Calculator.GetContextNodes(false);
                    return NodeSetFunctions.name(context, contextNodes);
                }
                else if (args.Count == 1)
                {
                    return NodeSetFunctions.name(context, args[0]);
                }
                else
                {
                    throw new ExprException("Wrong parameters in name().");
                }
            }
            else
            {
                return null;
            }
        }

    }
}
//CID 0797 //2011.02.28 //End